import { ref, onMounted } from 'vue'
import { getCategoryApi } from '@/apis/category'
import { useRoute } from 'vue-router'
import { onBeforeRouteUpdate } from 'vue-router'

export const useCategory = () => {
  const categoryData = ref({})
  const route = useRoute()
  const getCategory = async (id = route.params.id) => {
    const res = await getCategoryApi(id)
    categoryData.value = res.result
  }
  onMounted(() => getCategory())
  onBeforeRouteUpdate((to) => getCategory(to.params.id))
  return {
    categoryData,
  }
}
